<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.w3.org/1999/xhtml">
<head>
	<meta charset="UTF-8">
	<meta name="viewport"
				content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"/>
	<title>操作日志</title>

	<!-- lay ui css -->
	<link th:href="@{/component/layui/css/layui.css}" rel="stylesheet">
	<!-- 自定义 css -->
	<link rel="stylesheet" th:href="@{/dimples/css/dimples.css}"/>

</head>
<body>
<div class="layui-fluid layui-anim dimples-anim" id="dimples-log" lay-title="操作日志">
	<div class="layui-row dimples-container">
		<div class="layui-col-md12">
			<div class="layui-card">
				<div class="layui-card-body dimples-table-full">
					<form class="layui-form layui-table-form" lay-filter="log-table-form">
						<div class="layui-row">
							<div class="layui-col-md10">
								<div class="layui-form-item">
									<div class="layui-inline">
										<label class="layui-form-label layui-form-label-sm">操作人</label>
										<div class="layui-input-inline">
											<label>
												<input type="text" name="username" autocomplete="off" class="layui-input">
											</label>
										</div>
									</div>
									<div class="layui-inline">
										<label class="layui-form-label layui-form-label-sm">操作时间</label>
										<div class="layui-input-inline">
											<label for="log-createTime"></label>
											<input type="text" name="createTime" id="log-createTime" class="layui-input">
										</div>
									</div>
									<div class="layui-inline">
										<label class="layui-form-label layui-form-label-sm">操作描述</label>
										<div class="layui-input-inline">
											<label>
												<input type="text" name="operation" autocomplete="off" class="layui-input">
											</label>
										</div>
									</div>
								</div>
							</div>
							<div class="layui-col-md2 table-action-area">
								<div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="query">
									<i class="layui-icon">&#xe848;</i>
								</div>
								<div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="reset">
									<i class="layui-icon">&#xe79b;</i>
								</div>
								<div class="layui-btn layui-btn-sm layui-btn-primary table-action action-more"
										 shiro:hasAnyPermissions="ops-log:delete,ops-log:export">
									<i class="layui-icon">&#xe875;</i>
								</div>
							</div>
						</div>
					</form>
					<table lay-filter="logTable" lay-data="{id: 'logTable'}"></table>
				</div>
			</div>
		</div>
	</div>
</div>
<script type="text/html" id="log-time">
	<div>
		{{# if (d.time < 500) { }}
		<span class="layui-badge dimples-tag-green">{{d.time}} ms</span>
		{{# } else if (d.time < 1000) { }}
		<span class="layui-badge dimples-tag-cyan">{{d.time}} ms</span>
		{{# } else if (d.time < 1500) { }}
		<span class="layui-badge dimples-tag-orange">{{d.time}} ms</span>
		{{# } else { }}
		<span class="layui-badge dimples-tag-red">{{d.time}} ms</span>
		{{# } }}
	</div>
</script>
<script type="text/html" id="log-option">
	<span shiro:lacksPermission="ops-log:delete">
        <span class="layui-badge-dot dimples-bg-orange"></span> 无权限
    </span>
	<a lay-event="del" shiro:hasPermission="ops-log:delete"><i
					class="layui-icon dimples-edit-area dimples-red">&#xe7f9;</i></a>
</script>
</body>

<!-- lay ui js -->
<script type="text/javascript" th:src="@{/component/layui/layui.js}"></script>
<!-- 页面处理 -->
<script th:inline="javascript">
    var currentUser = {};
    currentUser.roleSet = [[${roles}]];
    currentUser.permissionSet = [[${permissions}]];
    console.log("用户角色：" + currentUser.roleSet);
    console.log("用户权限：" + currentUser.permissionSet);
</script>
<script data-th-inline="none" type="text/javascript">
    layui.use(['jquery', 'laydate', 'form', 'table', 'pearDimples', 'dropdown'], function () {
        var $ = layui.jquery,
            laydate = layui.laydate,
            dimples = layui.pearDimples,
            form = layui.form,
            table = layui.table,
            dropdown = layui.dropdown,
            $view = $('#dimples-log'),
            $query = $view.find('#query'),
            $reset = $view.find('#reset'),
            $searchForm = $view.find('form'),
            sortObject = {field: 'time', type: null},
            tableIns,
            createTimeFrom,
            createTimeTo;

        laydate.render({
            elem: '#log-createTime',
            range: true,
            trigger: 'click'
        });

        form.render();
        initTable();

        dropdown.render({
            elem: $view.find('.action-more'),
            click: function (name, elem, event) {
                if (name === 'delete') {
                    var checkStatus = table.checkStatus('logTable');
                    if (!checkStatus.data.length) {
                        dimples.alert.warn('请勾选需要删除的日志');
                    } else {
                        dimples.modal.confirm('删除日志', '确定删除所选日志？', function () {
                            var logIds = [];
                            layui.each(checkStatus.data, function (key, item) {
                                logIds.push(item.id)
                            });
                            deleteLogs(logIds.join('|'))
                        });
                    }
                }
                if (name === 'export') {
                    var params = $.extend(getQueryParams(), {
                        field: sortObject.field,
                        order: sortObject.type
                    });
                    params.pageSize = $view.find(".layui-laypage-limits option:selected").val();
                    params.pageNum = $view.find(".layui-laypage-em").next().html();
                    dimples.download('/opsLog/excel', params, '操作日志表.xlsx');
                }
            },
            options: [{
                name: 'delete',
                title: '删除日志',
                perms: 'ops-log:delete'
            }, {
                name: 'export',
                title: '导出Excel',
                perms: 'ops-log:export'
            }]
        });

        table.on('tool(logTable)', function (obj) {
            var data = obj.data,
                layEvent = obj.event;
            if (layEvent === 'del') {
                dimples.modal.confirm('删除日志', '确定删除该条系统日志？', function () {
                    deleteLogs(data.id);
                });
            }
        });

        table.on('sort(logTable)', function (obj) {
            sortObject = obj;
            tableIns.reload({
                initSort: obj,
                where: $.extend(getQueryParams(), {
                    field: obj.field,
                    order: obj.type
                })
            });
        });

        $query.on('click', function () {
            var params = $.extend(getQueryParams(), {
                field: sortObject.field,
                order: sortObject.type
            });
            tableIns.reload({where: params, page: {curr: 1}});
        });

        $reset.on('click', function () {
            $searchForm[0].reset();
            sortObject.type = 'null';
            createTimeTo = null;
            createTimeFrom = null;
            tableIns.reload({where: getQueryParams(), page: {curr: 1}, initSort: sortObject});
        });

        function initTable() {
            tableIns = dimples.table.init({
                elem: $view.find('table'),
                id: 'logTable',
                url: '/opsLog/list',
                cols: [[
                    {type: 'checkbox'},
                    {field: 'username', title: '操作人'},
                    {field: 'operation', title: '操作描述'},
                    {field: 'time', title: '耗时', templet: "#log-time", sort: true},
                    {field: 'method', title: '操作方法'},
                    {field: 'params', title: '方法参数'},
                    {field: 'ip', title: 'IP地址'},
                    {field: 'location', title: '操作地点'},
                    {field: 'createTime', title: '创建时间', minWidth: 180, sort: true},
                    {title: '操作', toolbar: '#log-option'}
                ]]
            });
        }

        function deleteLogs(logIds) {
            dimples.post('/opsLog/delete/' + logIds, null, function () {
                dimples.alert.success('删除系统日志成功');
                $query.click();
            });
        }

        function getQueryParams() {
            var createTime = $searchForm.find('input[name="createTime"]').val();
            if (createTime) {
                createTimeFrom = createTime.split(' - ')[0];
                createTimeTo = createTime.split(' - ')[1];
            }
            return {
                createTimeFrom: createTimeFrom,
                createTimeTo: createTimeTo,
                username: $searchForm.find('input[name="username"]').val().trim(),
                operation: $searchForm.find('input[name="operation"]').val().trim(),
                invalidate_ie_cache: new Date()
            };
        }
    })
</script>
</html>